<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="915" height="625" xmlns:ns1="com.trisun.common.*" 
	xmlns:ns2="com.trisun.dws.common.*" backgroundColor="#ffffff" initialize="initComponent();" xmlns:ns3="com.trisun.*" xmlns:ns4="com.trisun.common.*"
	 scroll="validateNow();invalidateDisplayList();" xmlns:ns6="*" fontSize="12">
	
	<!--
	<mx:RemoteObject id="dictProxy" destination="DictProxy">
		<mx:method name="getAge" result="fillAgeStr(event);" fault="RemoteErrHandler.handle(event);"/>
	</mx:RemoteObject>
	
	--> 

 
 	<mx:RemoteObject id="commonDao" destination="CommonEntityManager">
		<mx:method name="find" result="fillInfo(event)" fault="RemoteErrHandler.handle(event);"/>
		<mx:method name="saveList" result="afterSave(event)" fault="afterSaveError(event)"/>
	</mx:RemoteObject> 
	
	<mx:RemoteObject id="commonDao1" destination="CommonEntityManager">
		<mx:method name="saveList" result="afterDelete(event)" fault="RemoteErrHandler.handle(event);"/>
	</mx:RemoteObject> 
	
	<mx:RemoteObject id="remoteDictService" destination="RemoteDictService">
 		<mx:method name="findBySqltag" result="ddGrid.EditId.fillRetData(event)" fault="RemoteErrHandler.handle(event)"/>
 	</mx:RemoteObject> 
 	  	
	<mx:Script>
 	
		<![CDATA[
		import mx.formatters.DateFormatter;
		import com.trisun.dws.GlobalVar;
		import mx.rpc.events.ResultEvent;
		import mx.controls.Alert;
		import com.trisun.common.BaseVo;
		import mx.utils.StringUtil;
		import com.trisun.common.RemoteErrHandler;
		import com.trisun.dws.common.HashMap;
		import mx.collections.ArrayCollection;
		import com.trisun.dws.pat.ViewEnc;
		import com.trisun.util.FunctionUtil;
		import mx.rpc.events.FaultEvent;
		import com.trisun.dws.pmi.data.Employee;
		import com.trisun.dws.pmi.data.Organization;
		import mx.events.CloseEvent;
		import com.trisun.dws.main.DwsFrame;
		
		[Bindable] public var ${table.className?uncap_first}Card:${table.className}Card=new ${table.className}Card();
		[Bindable] public var cardList:ArrayCollection=new ArrayCollection();
		[Bindable] public var isMutiple:Boolean = false;
		public var cnt:String = "0";
		<#list table.getColumns() as column>
		<#if (column.ui=="Combobox")>
		[Bindable] public var ${column.javaProperty}List:ArrayCollection = new ArrayCollection(${column.dict});//定义数组 
		</#if>
		<#if (column.ui=="RadioButton")>
		</#if>
		</#list>
		
			/**
 		* 初始化组件

		 */ 
		public function initComponent(flag:Boolean=false):void{
			btnAdd.enabled = true;
			btnSave.enabled=true;
			cardList = new ArrayCollection();
			${table.className?uncap_first}Card = new ${table.className}Card();
			isMutiple = flag;
			
/* 			empId = GlobalVar.currentUser.employeeId;
			empName = GlobalVar.currentUser.employee.legalName;
			reportArea = GlobalVar.sysParam.reportArea;
			reportAreaName = GlobalVar.sysParam.reportAreaName;
			sysHospital = GlobalVar.sysParam.sysHospital; */
			var admissTimes:int;
			
			if(GlobalVar.currentEncounter.code=="onp"){
				admissTimes = GlobalVar.currentEncounter.outVisitTimes;
			}else{
				admissTimes = GlobalVar.currentEncounter.inVisitTimes;
			}
			
			
			commonDao.find("from ${table.className} where patientId='"+GlobalVar.currentPatient.patientId+"' and admissTimes="+admissTimes);	
		}
		
		private function fillInfo(event:ResultEvent):void{
			cardList = event.result as ArrayCollection;
			if (cardList.length==0) {
				callLater(addInfoNew);
				btnAdd.enabled = false;
			}else{
				cardList = event.result as ArrayCollection;
				
				if(cnt=="0"){
				${table.className?uncap_first}Card = cardList[0] as ${table.className}Card;
				}else{
					var f:Boolean = true;
					for(var i:int=0; i<cardList.length; i++){
						var card:${table.className}Card = cardList[i] as ${table.className}Card;
						if(card.cardNo==cnt){
							cbSel.selectedItem = card;
							${table.className?uncap_first}Card = card;
							f = false; 
							break;
						}
					}
					if(f){ 
						cnt = "0";
						${table.className?uncap_first}Card = cardList[0] as ${table.className}Card;
					}
				}
				
				<#list table.getColumns() as column>
				<#if (column.ui=="Combobox")>
				//填充Combobox
				for(var i:int=0; i<${column.javaProperty}List.length; i++){
					if(${column.javaProperty}List[i].value==${table.className?uncap_first}Card.${column.javaProperty}){
						${column.javaProperty}.selectedIndex = i;
					}
				}
				</#if>
				<#if (column.ui=="RadioButton")>
				</#if>
				</#list>
					
			}
		}
		
		public function addInfoNew():void{
			${table.className?uncap_first}Card = new ${table.className}Card();
			
			${table.className?uncap_first}Card.patientId = GlobalVar.currentPatient.patientId;
				
			if(GlobalVar.currentEncounter.code=="onp"){
				${table.className?uncap_first}Card.admissTimes = GlobalVar.currentEncounter.outVisitTimes;
			}else{
				${table.className?uncap_first}Card.admissTimes = GlobalVar.currentEncounter.inVisitTimes;	
			}
			
			
			${table.className?uncap_first}Card._rowStatus = BaseVo.STATUS_NEW;
			
			
			${table.className?uncap_first}Card.patientName = GlobalVar.currentPatient.legalName;
			//${table.className?uncap_first}Card.birthday = GlobalVar.currentPatient.birthTime;
			${table.className?uncap_first}Card.sex = GlobalVar.currentPatient.administrativeGenderCode;
			//年龄
			//dictProxy.getAge(GlobalVar.currentPatient.birthTime);
			/*
			//身份证号
			${table.className?uncap_first}Card.socialNo = GlobalVar.currentPatient.socialNo;
			${table.className?uncap_first}Card.relationTel = GlobalVar.currentPatient.telAddr;
			//报告科室
			${table.className?uncap_first}Card.reportDept = GlobalVar.currentEncounter.respPartyId;
			${table.className?uncap_first}Card.reportDeptName = GlobalVar.currentEncounter.deptName;
			${table.className?uncap_first}Card.effectiveB = GlobalVar.currentEncounter.effectiveTimeB;
			${table.className?uncap_first}Card.diagnose = GlobalVar.currentEncounter.diagnos;
			${table.className?uncap_first}Card.deptSn = GlobalVar.currentEncounter.deptName;
			${table.className?uncap_first}Card.authorName = GlobalVar.currentUser.employee.legalName;
			${table.className?uncap_first}Card.visitNo = GlobalVar.currentEncounter.code=="onp"?GlobalVar.currentPatient.outPatientNo:GlobalVar.currentPatient.inPatientNo;
			*/
			${table.className?uncap_first}Card.author = GlobalVar.currentUser.employeeId;
			//${table.className?uncap_first}Card.reportDate = new Date();
			
			if (cardList != null && cardList.length>0) {
				${table.className?uncap_first}Card.cardNo = (int(cardList.getItemAt(cardList.length-1).cardNo) + 1).toString();
				cardList.addItemAt(${table.className?uncap_first}Card,cardList.length);
				cbSel.selectedIndex=cardList.length-1;
			} else {
				${table.className?uncap_first}Card.cardNo = "1";
				cardList.addItemAt(${table.className?uncap_first}Card,0);
			}
			cnt = ${table.className?uncap_first}Card.cardNo;
			
		}
		/*
		private function fillAgeStr(event:ResultEvent):void{
			var ageStr:String=event.result as String;
			if(${table.className?uncap_first}Card==null||ageStr=="") return;
			var ageUnit:String=ageStr.substr(ageStr.indexOf("-")+1,ageStr.length-ageStr.indexOf("-"));
			${table.className?uncap_first}Card.age=Number(ageStr.substr(0,ageStr.indexOf("-")));
			//${table.className?uncap_first}Card.ageUnit=ageStr.substr(ageStr.indexOf("-")+1,ageStr.length-ageStr.indexOf("-"));
		}
		*/
		
		public function infoChange(name:String):void{
			
			if(${table.className?uncap_first}Card==null) return;
			
			<#list table.getColumns() as column>
			<#if (column.display)>
			   //${column.remarks}
				if(name=="${column.javaProperty}"){
					${table.className?uncap_first}Card.${column.javaProperty}=
					<#if (column.ui=="TextInput")>
						<#if (column.getFlexType()=="int"||column.getFlexType()=="Number")>
						Number(${column.javaProperty}.text);
						<#else>
						StringUtil.trim(${column.javaProperty}.text);
						</#if>
					</#if>
					<#if (column.ui=="FormatDateField")>${column.javaProperty}.selectedDate;</#if>
					<#if (column.ui=="TextAreas")>StringUtil.trim(${column.javaProperty}.text);</#if>
					<#if (column.ui=="Combobox")>String(${column.javaProperty}.selectedItem.value);</#if>
					<#if (column.ui=="RadioButton")>String(${column.javaProperty}.selectedValue);</#if>
					<#if (column.ui=="CheckBox")>${column.javaProperty}.selected?"1":"0";</#if>
					<#if (column.ui=="DateTime")>${column.javaProperty}.selectedDate;</#if>
				}
			</#if>	
			</#list>
			
			
			
		}
		
		private function changeCard(): void {
			${table.className?uncap_first}Card = cbSel.selectedItem as ${table.className}Card;
			cnt = cbSel.selectedItem.cardNo;
			
		}
		
		//校验
		public function checkInfo():Boolean{
		var msg:String ="";
		<#list table.getColumns() as column>
		<#if (column.display)>
			<#if (!column.nullable)>
				<#if (column.ui=="TextInput")>
				//${column.remarks}
				if(${column.javaProperty}.text==null||$StringUtil.trim({column.javaProperty}.text)==""){
					msg += "${column.remarks}必须填写 \r\n";
				}
				</#if>
				<#if (column.ui=="FormatDateField")>
				if(${column.javaProperty}.selectedDate==null){
					msg += "${column.remarks}必须填写 \r\n";
				}
				</#if>
				<#if (column.ui=="TextAreas")>
				if(${column.javaProperty}.text==null||$StringUtil.trim({column.javaProperty}.text)==""){
					msg += "${column.remarks}必须填写 \r\n";
				}
				</#if>
				<#if (column.ui=="Combobox")>
				if(${column.javaProperty}.selectedItem==null){
					msg += "${column.remarks}必须填写 \r\n";
				}
				</#if>
				<#if (column.ui=="RadioButton")>
				if(${column.javaProperty}.selectedValue==null){
					msg += "${column.remarks}必须填写 \r\n";
				}
				</#if>
				<#if (column.ui=="DateTime")>
				if(${column.javaProperty}.selectedDate==null){
					msg += "${column.remarks}必须填写 \r\n";
				}
				</#if>
			</#if>
		</#if>
		</#list>
		if(msg!=""){
			Alert.show(msg);
			return false;
		}
			return true;	
		}
		
		public function saveCard():void{
			var saveList:ArrayCollection = new ArrayCollection();
			if(!checkInfo()) return;
			saveList.addItem(${table.className?uncap_first}Card);
			commonDao.saveList(saveList);
		}
		
		private function afterSave(event:ResultEvent):void{
		
			initComponent(isMutiple);
			Alert.show("操作成功！");
		}

		private function afterSaveError(event:FaultEvent):void{
			btnSave.enabled=true;
			Alert.show("操作失败!");
		}
		
		private function deleteCard():void{
			Alert.show("确定是否删除","提示",(Alert.YES |Alert.NO), null,
				function(event:CloseEvent):void{
		 			if (event.detail==Alert.YES){
						deleteCardExecute();
		 			}
		 		}
		 	);
		}

		private function deleteCardExecute():void{
			if(${table.className?uncap_first}Card._rowStatus!=BaseVo.STATUS_NEW){
				${table.className?uncap_first}Card._rowStatus = BaseVo.STATUS_DEL;
				var saveList:ArrayCollection = new ArrayCollection();
				saveList.addItem(${table.className?uncap_first}Card);
				commonDao1.saveList(saveList);
			}
		}
		
		private function afterDelete(event:ResultEvent):void{
			Alert.show("删除成功！");
			initComponent(isMutiple);
		}

		private function dispDate(item:Object, column:DataGridColumn):String{
			var formatter:DateFormatter=new DateFormatter;
			formatter.formatString="YYYY-MM-DD JJ:NN";
			return formatter.format(item[column.dataField]);
		}
			
		]]>
	</mx:Script>
		
	<mx:LinkBar id="lbType" dataProvider="vsMain" disabledColor="#0000ff" borderStyle="solid" horizontalGap="0"  
		y="5" >
	</mx:LinkBar>
	<mx:Label text="这里填写报卡名称"  textAlign="center" y="2" fontWeight="bold" fontSize="20" x="112"/>
	<mx:ViewStack id="vsMain" width="100%" height="100%"  y="35" borderStyle="solid" backgroundColor="white">
	<#list table.getPages() as page>
		<mx:Canvas width="100%" height="100%"  label="第${page_index+1}页" backgroundColor="white">
		<#if (page_index==0)>
			<mx:HRule x="21" y="56" width="845"  alpha="0.2" themeColor="#000000" strokeColor="#000000"/>
			<mx:HRule x="19" y="557" width="845"  alpha="0.2" themeColor="#000000" strokeColor="#000000"/>
			<mx:VRule x="19" y="56" height="502" strokeColor="#000000" themeColor="#000000" alpha="0.5"/>
			<mx:VRule x="864" y="57" height="501" strokeColor="#000000" themeColor="#000000" alpha="0.5"/>
			<mx:Label x="26" y="31" text="卡片编号："  width="72"/>
			<mx:TextInput x="106" y="31" width="137" id="cardNo" color="#000000"  borderSides="bottom" borderStyle="solid" 
				text="{${table.className?uncap_first}Card.cardNo}" change="infoChange('cardNo');" editable="false" tabIndex="1"/>
			<mx:Label x="274" y="31" text="报告日期："  width="72"/>
			<ns2:FormatDateField x="346" y="29" width="105" id="reportDate" selectedDate="{${table.className?uncap_first}Card.reportDate}" change="infoChange('reportDate');"
			  disabledColor="black" tabIndex="33" enabled="true" />
		</#if>  
			<#list page as column>
			
			<#if (column.display)>
			<#if (column.ui!="CheckBox")>
				<#if (column.remarks?length>0)>
				<mx:Label x="${column.lableX}" y="${column.lableY}" text="${column.remarks}："  color="#0000ff"/>
				</#if>
			</#if>
			<#if (column.ui=="TextInput")>
			<mx:TextInput x="${column.x}" y="${column.y}" width="${column.width}" id="${column.javaProperty}" color="#000000"  borderSides="bottom" borderStyle="solid" 
		text="{${table.className?uncap_first}Card.${column.javaProperty}}" change="infoChange('${column.javaProperty}');" editable="${column.editable?string('true','false')}" />
			</#if>
			<#if (column.ui=="FormatDateField")>
			<ns2:FormatDateField x="${column.x}" y="${column.y}"  id="${column.javaProperty}" selectedDate="{${table.className?uncap_first}Card.${column.javaProperty}}" change="infoChange('${column.javaProperty}');"
			  disabledColor="black"  enabled="${column.editable?string('true','false')}" />
			</#if>
			<#if (column.ui=="TextAreas")>
			<mx:TextArea x="${column.x}" y="${column.y}" width="${column.width}" height="${column.height}" borderSides="bottom" borderStyle="solid" id="${column.javaProperty}" text="{${table.className?uncap_first}Card.${column.javaProperty}}" change="infoChange('${column.javaProperty}');" editable="${column.editable?string('true','false')}"/>
			</#if>
			<#if (column.ui=="Combobox")>
			<mx:ComboBox x="${column.x}" y="${column.y}" id="${column.javaProperty}" dataProvider="{${column.javaProperty}List}" change="infoChange('${column.javaProperty}');" enabled="${column.editable?string('true','false')}"></mx:ComboBox>
			</#if>
			<#if (column.ui=="RadioButton")>
			<mx:RadioButtonGroup id="${column.javaProperty}" itemClick="infoChange('${column.javaProperty}');" enabled="${column.editable?string('true','false')}"/>
			<#assign x=column.x+15 />
			<#list column.getRadioButtonDate() as radioButton>
			<mx:RadioButton x="${x}" y="${column.y}" label="${radioButton["label"]!}" groupName="${column.javaProperty}"  selected="{${table.className?uncap_first}Card.${column.javaProperty}=='${radioButton["value"]!}'}" value="${radioButton["value"]!}"/>
			<#assign x=x + 15*(radioButton["label"]?length+2) />
			</#list>
			</#if>
			<#if (column.ui=="CheckBox")>
			<mx:CheckBox x="${column.x}" y="${column.y}" id="${column.javaProperty}" label="${column.remarks}" selected="{${table.className?uncap_first}Card.${column.javaProperty}=='1'}" change="infoChange('${column.javaProperty}');" enabled="${column.editable?string('true','false')}"/>
			</#if>
			<#if (column.ui=="DateTime")>
			 <ns4:MaskedInput  inputMask="####-##-## ##:##"  x="${column.x}" y="${column.y}"   id="${column.javaProperty}"  
				           change="infoChange('${column.javaProperty}');"  selectedDate="{{${table.className?uncap_first}Card.${column.javaProperty}}"  
				        	tabIndex="11" enabled="{opRecordEnable}"  width="128"/>
			</#if>
			
			</#if>
			</#list>
			
		</mx:Canvas>
	</#list>

	</mx:ViewStack>	
	<mx:Label x="589" y="11" id="lbSel" text="卡号："/>	
	<mx:ComboBox x="626" y="8" id="cbSel" width="83" dataProvider="{cardList}" labelField="cardNo" change="changeCard()"/>		
	<mx:Button x="721" y="7" label="新增" id="btnAdd" click="addInfoNew()" />
	<mx:Button x="837" y="7" label="保存" id="btnSave" click="saveCard();"/>
	<mx:Button x="779" y="7" label="删除" id="btnDelete" click="deleteCard();" enabled="{cbSel.selectedItem._rowStatus!='NEW'}" visible="{cbSel.selectedItem._rowStatus!='NEW'}"/>
	<ns3:TsInputGrid id="ddGrid" width="125" height="149" fontSize="12"  autoAdjustParent="false" visible="false" x="213" y="55"/>

</mx:Canvas>
